package com.ruoyi.web.controller.inspection;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inspection.domain.TPeople;
import com.ruoyi.inspection.service.ITPeopleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人员Controller
 *
 * @author ruoyi
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/inspection/people")
public class TPeopleController extends BaseController {
    @Autowired
    private ITPeopleService tPeopleService;

    /**
     * 查询人员列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:people:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPeople tPeople) {
        startPage();
        List<TPeople> list = tPeopleService.selectTPeopleList(tPeople);
        for (TPeople people : list) {
            people.getSalaryType();
        }
        return getDataTable(list);
    }

    /**
     * 导出人员列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:people:export')")
    @Log(title = "人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TPeople tPeople) {
        List<TPeople> list = tPeopleService.selectTPeopleList(tPeople);
        ExcelUtil<TPeople> util = new ExcelUtil<TPeople>(TPeople.class);
        util.exportExcel(response, list, "人员数据");
    }

    /**
     * 获取人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('inspection:people:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tPeopleService.selectTPeopleById(id));
    }

    /**
     * 新增人员
     */
    @PreAuthorize("@ss.hasPermi('inspection:people:add')")
    @Log(title = "人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPeople tPeople, @AuthenticationPrincipal UserDetails userDetails) {
        tPeople.setId(IdUtils.randomUUID());
        tPeople.setDeleteFlag(Constants.INT_ZERO);
        tPeople.setAddTime(new Date());
        tPeople.setAddBy(userDetails.getUsername());
        return toAjax(tPeopleService.insertTPeople(tPeople));
    }

    /**
     * 修改人员
     */
    @PreAuthorize("@ss.hasPermi('inspection:people:edit')")
    @Log(title = "人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPeople tPeople, @AuthenticationPrincipal UserDetails userDetails) {
        tPeople.setUpdateTime(new Date());
        tPeople.setUpdateBy(userDetails.getUsername());
        int updateFlag = tPeopleService.updateTPeople(tPeople);
        if (updateFlag > 0) {
            return success(tPeople);
        }
        return error("修改失败");
    }

    /**
     * 删除人员
     */
    @PreAuthorize("@ss.hasPermi('inspection:people:remove')")
    @Log(title = "人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tPeopleService.deleteTPeopleByIds(ids));
    }
}
